MyModules.define('editModal', [], function () {
	var id = 0;
	var activeType;
	window.editModal;
	function render() {
		var str = '<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">';
		str += '<div class="modal-dialog">';
		str += '	<div class="modal-content">';
		str += '		<div class="modal-header">';
		str += '			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>';
		str += '			<h4 class="modal-title" id="myModalLabel">模态框（Modal）标题</h4>';
		str += '		</div>';
		str += '		<div class="modal-body">在这里添加一些文本</div>';
		str += '		<div class="modal-footer">';
		str += '			<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>';
		str += '			<button type="button" class="btn btn-primary" data-type="commit">確認</button>';
		str += '		</div>';
		str += '	</div><!-- /.modal-content -->';
		str += '</div><!-- /.modal -->';
		str += '</div>';
		return str;
	}
	function flexTemplate(content) {
		id++;
		var str = `<div class="editorContainer" id="editorContainer-${id}">
		<div class="mask"></div>
		<div class="editor-inner">
		${content}
		</div>
	</div>`;
		$("body").append(str);
	}
	function show(type) {
		if (type) {
			this.activeType = type;
			flexTemplate(template(type.datas.name, type.render()));
			if (typeof type.registerJs == 'function') {
				type.registerJs();
			}
			window.editModal = this;
		}

	}
	function template(title, content) {
		var str = '<div id="myModal">	<div class="modal-content">';
		str += '		<div class="modal-header">';
		str += '			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>';
		str += '			<h4 class="modal-title" id="myModalLabel">' + title + '</h4>';
		str += '		</div>';
		str += '		<div class="modal-body">' + content + '</div>';
		str += '		<div class="modal-footer">';
		str += '			<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>';
		str += '			<button type="button" class="btn btn-primary" data-type="commit">確認</button>';
		str += '		</div>';
		str += '	</div><!-- /.modal-content --></div>';
		return str;
	}
	function hide() {
		if($("body > .editorContainer").is(":visible")){
			$("body > .editorContainer").remove();
			if(typeof(activeType)!="undefined" && typeof(activeType.close)!="undefined"){
				activeType.close();
				window.editModal = null;
			}
		}
	}

	$(document).on("click", "#myModal [data-dismiss='modal']", function () {
		var editModal = window.editModal;
		editModal.hide();
	});
	// 模塊保存按鈕事件
	$(document).on('click', '#myModal [data-type="commit"]', function(){
		// tinymce.activeEditor.save();
		tinymce.editors.forEach(function(editor) {
		    editor.save(); // 保存每个编辑器的内容
		    // 你可以在这里处理编辑器的内容，例如发送到服务器
		 });
		var editModal = window.editModal;
		var control = editModal.activeType;
		if(typeof(control.saveData) == 'function'){
			control.saveData();
		}
		editModal.hide();
		dragObj.ChangeControl(control);
	});
	//清空
	$(document).on('click', '.ldgFormClearAll', function () {
		dragObj.clear();
	})
	//修改數據
	$(document).on('click', '.controlEdit', function (e) {
		e.stopPropagation() //阻止事件冒泡
		let id = $(this).parent().data('target') //点击的元素类型
		if(!empty(id)){
			var editModal = MyModules.get('editModal');
			editModal.show(dragObj.getModule(id));
			WarpLabel.hide();
		}
	});
	return {
		name: '彈窗', //控件名称
		type: 'editModal',
		render: render,
		activeType:activeType,
		show: show,
		hide: hide
	}
});
